import {useCallback} from 'react';
import {TypedUseSelectorHook, useDispatch, useSelector} from 'react-redux';
import {
  loginUser,
  logout,
  fetchUserInfo,
  fetchCaptcha,
  addUserThunk,
  updateUserThunk, userAddAgentsThunk, userDeleteAgentsThunk
} from '@/services/userService';
import {LoginData, SysUserAddReq, SysUserInfoReq,  UserAgentsReq, UserInfo} from '@/types/userType';
import {AppDispatch, RootState} from "@/stores";
import {resetUserInfo, setUserInfo} from "@/stores/userSlice";


// Custom Hook to handle user-related dispatch actions
export function useDispatchUser() {
    const dispatch = useDispatch<AppDispatch>();

    // Login action
    const login = useCallback((loginData: LoginData) => {
        return dispatch(loginUser(loginData));
    }, [dispatch]);

    // Logout action
    const logoutUser = useCallback(() => {
        dispatch(logout());
    }, [dispatch]);

    // Fetch user information
    const fetchUser = useCallback(() => {
        return dispatch(fetchUserInfo());
    }, [dispatch]);

    // Add a new user
    const addUser = useCallback((userData: SysUserAddReq) => {
        return dispatch(addUserThunk(userData));
    }, [dispatch]);


    // Update existing user
    const updateUser = useCallback((username: string, data: SysUserInfoReq) => {
        return dispatch(updateUserThunk({username, data}));
    }, [dispatch]);


    // Set partial user info
    const setUser = useCallback((info: Partial<UserInfo>) => {
        dispatch(setUserInfo(info));
    }, [dispatch]);

    // Get captcha image
    const getCaptcha = useCallback(() => {
        return dispatch(fetchCaptcha());
    }, [dispatch]);

    // Reset user information
    const resetUser = useCallback(() => {
        dispatch(resetUserInfo());
    }, [dispatch]);

    // Reset user information
    const addUserAgent = useCallback((data: UserAgentsReq) => {
      return dispatch(userAddAgentsThunk(data));
    }, [dispatch]);

  // Reset user information
  const deleteUserAgent = useCallback((data: UserAgentsReq) => {
    return  dispatch(userDeleteAgentsThunk(data));
  }, [dispatch]);


  return {
      login,
      logoutUser,
      fetchUser,
      addUser,
      updateUser,
      setUser,
      resetUser,
      getCaptcha,
      addUserAgent,
      deleteUserAgent
  };
}

// Create a typed useSelector hook
export const useUserSelector: TypedUseSelectorHook<RootState> = useSelector;
